//
//  ViewController.m
//  魔法砖
//
//  Created by apple on 15/8/25.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"
#import "Rectangle.h"

@interface ViewController ()

{

    NSMutableArray *_rectArray;
    NSMutableArray *_viewArray;
    NSArray *_colorArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _rectArray = [NSMutableArray array];
    _viewArray = [NSMutableArray array];
    _colorArray = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
    
//    读文件时，有可能文件不存在或者文件内容为空／甚至文件格式不对
    NSData *data = [NSData dataWithContentsOfFile:@"Users/apple/Desktop/test.json"];
    
    if (data) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil];
        
//        快速枚举；
        [array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
            Rectangle *rectangle = [Rectangle rectangleFormDictionary:dict];
            [_rectArray addObject:rectangle];
            [self createView:rectangle];
            
        }];
    }
    
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - 点击保存
- (IBAction)didSaveClick:(UIButton *)sender {
    
    NSMutableArray *array = [NSMutableArray array];
    
//    将数组中的所有Rectangle类型的对象全部转换成NSDictionary类型的对象；
    for (Rectangle *rectangle in _rectArray)
    {
        NSDictionary *dict =[rectangle dictionary];
        
        [array addObject:dict];
    }
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:array options:NSUTF8StringEncoding error:nil];
    
    [data writeToFile:@"Users/apple/Desktop/test.json" atomically:YES];
    
}
#pragma mark - 点击撤销
- (IBAction)didUndoClick:(UIButton *)sender
{
//    [_viewArray removeLastObject];
    
    [_rectArray removeLastObject];
    UIView *v = [_viewArray lastObject];
    [v removeFromSuperview];
    [_viewArray removeLastObject];
}
#pragma mark - 点击删除
- (IBAction)didDeleteClick:(UIButton *)sender
{
    [_rectArray removeAllObjects];
    [_viewArray enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop)
    {
//        从屏幕上移除；
        [obj removeFromSuperview];
    }];
    [_viewArray removeAllObjects];
    
}

//触摸事件
#pragma mark - 触摸开始
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];//从触摸点中选取一个作为起始点；
    CGPoint point = [touch locationInView:self.view];//获取当前位置，
    
    //    把矩形的信息放入对象中保存起来；
    Rectangle *rectangle = [Rectangle new];
    rectangle.point = point;
    rectangle.color = arc4random_uniform((uint32_t)_colorArray.count);
    [_rectArray addObject:rectangle];
    
    //    构建一个矩形；
    CGRect rect;
    rect.origin = rectangle.point;
    rect.size = rectangle.size;
    
    //     创建一个矩形,设置矩形的颜色；
    UIView *v = [[UIView alloc] initWithFrame:rect];
    v.backgroundColor = _colorArray[rectangle.color];
    
    //     显示一个视图；
    [self.view addSubview:v];
    [_viewArray addObject:v];
}

#pragma mark - 移动

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self reshapeView:touches];
}

#pragma mark - 触摸结束；
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self reshapeView:touches];
}

- (void)reshapeView:(NSSet *)touches
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    
    //     读取数据；
    Rectangle *rectangle;
    rectangle = [_rectArray lastObject];//上面我们怎么存的就怎么取；
    
    //     构建矩形的大小；
    rectangle.size = CGSizeMake(point.x - rectangle.point.x, point.y - rectangle.point.y);
    
    //     构建矩形；
    CGRect rect;
    rect.origin = rectangle.point;
    rect.size = rectangle.size;
    
    //     将矩形显示出来；
    UIView *v = [_viewArray lastObject];
    v.frame = rect;
}

- (void)createView:(Rectangle *)rectangle
{
    CGRect rect;
    rect.origin = rectangle.point;
    rect.size = rectangle.size;
    
    UIView *v = [[UIView alloc] initWithFrame:rect];
    v.backgroundColor = _colorArray[rectangle.color];
    [self.view addSubview:v];
    [_viewArray addObject:v];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
